package cn.edu.sgu.www.mhxysy.service.partner.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.partner.PartnerDTO;
import cn.edu.sgu.www.mhxysy.entity.partner.Partner;
import cn.edu.sgu.www.mhxysy.mapper.partner.PartnerMapper;
import cn.edu.sgu.www.mhxysy.pager.partner.PartnerPager;
import cn.edu.sgu.www.mhxysy.service.partner.PartnerService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PartnerServiceImpl implements PartnerService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final PartnerMapper partnerMapper;

    @Autowired
    public PartnerServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            PartnerMapper partnerMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.partnerMapper = partnerMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_PARTNER;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(PartnerDTO partnerDTO) {
        Partner partner = toEntity(partnerDTO);

        partner.setId(null);

        partnerMapper.insert(partner);
    }

    @Override
    public void deleteById(Integer id) {
        partnerMapper.deleteById(id);
    }

    @Override
    public void updateById(PartnerDTO partnerDTO) {
        ObjectUtils.requireNonNull(partnerDTO.getId(), CustomMessages.ID_NOT_NULL);

        Partner partner = toEntity(partnerDTO);

        partnerMapper.updateById(partner);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<Partner> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                partnerMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<Partner> selectByType(Integer type) {
        QueryWrapper<Partner> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(
                type != null,
                "type", type
        );

        return partnerMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Partner> selectByPage(PartnerPager pager) {
        QueryWrapper<Partner> queryWrapper = new QueryWrapper<>();
        Page<Partner> page = Pager.ofPage(pager);

        queryWrapper.like(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );
        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );

        return partnerMapper.selectPage(page, queryWrapper);
    }

    private Partner toEntity(PartnerDTO partnerDTO) {
        Partner partner = new Partner();

        partner.setId(partnerDTO.getId());
        partner.setName(partnerDTO.getName());
        partner.setType(partnerDTO.getType());
        partner.setNote(partnerDTO.getNote());

        return partner;
    }

}